package com.heima.comment.service.impl;

import com.heima.comment.pojos.ApComment;
import com.heima.comment.service.CommentService;
import com.heima.model.comment.dtos.CommentDto;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.comment.dtos.CommentSaveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.pojo.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import thread.CommentThreadLocalUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存用户评论
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult commentSave(CommentSaveDto dto) {

        //判断是否登录
        ApUser user = CommentThreadLocalUtil.getUser();
        if (user == null || user.getId() == null) {
            return ResponseResult.errorResult(401, "未登录不能评论");
        }

        ApComment apComment = new ApComment();
        apComment.setEntryId(dto.getArticleId());
        apComment.setContent(dto.getContent());
        apComment.setReply(0);
        apComment.setLikes(0);
        apComment.setCreatedTime(new Date());
        apComment.setAuthorName("admin");
        mongoTemplate.save(apComment);
        return ResponseResult.okResult(200, "ok");
    }

    /**
     * 查看评论列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult commentLoad(CommentDto dto) {
        Query query = Query.query(Criteria.where("entryId").is(dto.getArticleId()).and("createdTime").lt(dto.getMinDate()))
                .with(Sort.by(Sort.Direction.DESC, "createdTime"));

        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(apComments);
        return responseResult;
    }

    /**
     * 点赞或者取消点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult commentLike(CommentLikeDto dto) {

        //查到要做修改的评论
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        Integer likes = apComment.getLikes();
        HashMap<String, Integer> map = new HashMap<>();
        ResponseResult<Object> responseResult = new ResponseResult<>();
        if (dto.getOperation() == 0) {
            apComment.setLikes(++likes);
            mongoTemplate.save(apComment);
            map.put("likes", likes);
            responseResult.setData(map);
            return responseResult;

        } else if (dto.getOperation() == 1) {
            likes--;
            apComment.setLikes(likes);
            mongoTemplate.save(apComment);
            map.put("likes", likes);
            responseResult.setData(map);
            return responseResult;
        } else {
            throw new RuntimeException("参数错误");
        }
    }
}
